//
//  XHRegisterViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/18.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHRegisterViewController.h"
#import "UINavigationController+YNBase.h"
#import "XHTabBarController.h"

#import "Services.h"
#import "AppDelegate.h"

@interface XHRegisterViewController ()

@property (weak, nonatomic) IBOutlet UILabel *ui_selectLabel;
@property (weak, nonatomic) IBOutlet UIView *ui_view1;
@property (weak, nonatomic) IBOutlet UIView *ui_view2;
@property (weak, nonatomic) IBOutlet UIView *ui_view3;
@property (weak, nonatomic) IBOutlet UIView *ui_view4;
@property (weak, nonatomic) IBOutlet UIView *ui_view5;

@property (weak, nonatomic) IBOutlet UIButton *ui_codeButton;
@property (weak, nonatomic) IBOutlet UITextField *ui_userTextField;
@property (weak, nonatomic) IBOutlet UITextField *ui_codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *ui_passWordTextField;
@property (weak, nonatomic) IBOutlet UITextField *ui_recCodeTextField;
@property (strong,nonatomic) NSDictionary *m_smsResultDic;
@property (assign, nonatomic) int waitingTime;
@property (strong, nonatomic) NSTimer *m_theTimer;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_height1;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_height2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_height3;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_height4;

@end

@implementation XHRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.la_height1.constant = 0.5;
    self.la_height2.constant = 0.5;
    self.la_height3.constant = 0.5;
    self.la_height4.constant = 0.5;
    
    self.ui_selectLabel.layer.masksToBounds = YES;
    self.ui_selectLabel.layer.cornerRadius = 10;
    self.ui_selectLabel.layer.borderWidth = 0.5;
    self.ui_selectLabel.layer.borderColor = [ColorManager colorWithHexString:@"#5997A4"].CGColor;
    
    self.ui_view1.layer.masksToBounds = YES;
    self.ui_view1.layer.cornerRadius = 8;
    self.ui_view1.layer.borderColor = [ColorManager colorWithHexString:@"#C4C4C4"].CGColor;
    self.ui_view1.layer.borderWidth = 0.5;
    
    self.ui_view3.layer.masksToBounds = YES;
    self.ui_view3.layer.cornerRadius = 8;
    self.ui_view3.layer.borderColor = [ColorManager colorWithHexString:@"#C4C4C4"].CGColor;
    self.ui_view3.layer.borderWidth = 0.5;
    
    self.ui_view4.layer.masksToBounds = YES;
    self.ui_view4.layer.cornerRadius = 8;
    self.ui_view4.layer.borderColor = [ColorManager colorWithHexString:@"#C4C4C4"].CGColor;
    self.ui_view4.layer.borderWidth = 0.5;
    
//    UIBarButtonItem *settingItem=[[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(presentAction:)];
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0,0,44,44)];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button addTarget:self action:@selector(presentAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *settingItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.rightBarButtonItem=settingItem;
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.ui_view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.ui_view2.bounds;
    maskLayer.path = maskPath.CGPath;
    maskLayer.fillColor=[UIColor clearColor].CGColor;
    maskLayer.strokeColor=[ColorManager colorWithHexString:@"#C4C4C4"].CGColor;
    maskLayer.lineWidth = 0.5;
    [self.ui_view2.layer addSublayer:maskLayer];
    
    UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:self.ui_view5.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    maskLayer1.frame = self.ui_view5.bounds;
    maskLayer1.path = maskPath1.CGPath;
    maskLayer1.fillColor=[UIColor clearColor].CGColor;
    maskLayer1.strokeColor=[ColorManager colorWithHexString:@"#C4C4C4"].CGColor;
    maskLayer1.lineWidth = 0.5;
    [self.ui_view5.layer addSublayer:maskLayer1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)successAction{
    AppDelegate *delegate=[UIApplication sharedApplication].delegate;
    [delegate.tabBarController goOwnViewController];;
    [self.navigationController popToRootViewControllerAnimated:YES];

}

//提交注册信息
- (void)presentAction:(UIButton*)button
{
    [self.view endEditing:YES];
    if ([self isCanLogin]) {
        NSString *smsId=self.m_smsResultDic[@"smsCode"];
//        if ([self.ui_userTextField.text isEqualToString:testAcount]) {
//            smsId=testSmsId;
//        }
        WS(weakSelf)
        [ServiceManager loginFirstWithPhone:self.ui_userTextField.text passwd:self.ui_passWordTextField.text code:self.ui_codeTextField.text smsId:smsId originalInviteCode:self.ui_recCodeTextField.text success:^(NSDictionary *data) {
            if ([[DataManager sharedInstance] isLogin]) {
            }
            if ([NSString checkNullString:self.ui_recCodeTextField.text])
            {
                [PromptAction showSuccessPrompt:@"注册成功"];
            }
            else
            {
                [PromptAction showSuccessPrompt:@"推荐码使用成功"];
            }
            [weakSelf successAction];
        } failure:^(NSDictionary * failure){
        }];
    }
}

- (void)viewDidDisappear:(BOOL)animated{
    [self.m_theTimer invalidate];
    self.m_theTimer=nil;
}

- (void)showSmsCodeWaitView{
    self.ui_codeButton.enabled=NO;
    self.waitingTime=60;
    [self.ui_codeButton setTitle:[NSString stringWithFormat:@"%d秒后重发",self.waitingTime] forState:UIControlStateDisabled];
    self.m_theTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateSmsCodeWait) userInfo:nil repeats:YES];
}

- (void)updateSmsCodeWait
{
    self.waitingTime--;
    NSString *title=[NSString stringWithFormat:@"%d秒后重发",self.waitingTime];
    self.ui_codeButton.titleLabel.text=title;
    [self.ui_codeButton setTitle:title forState:UIControlStateDisabled];
    if (self.waitingTime<=0) {
        [self hideSmsCodeWaitView];
    }
}

- (void)hideSmsCodeWaitView{
    [self.m_theTimer invalidate];
    [self.ui_codeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    self.ui_codeButton.enabled=YES;
}
- (BOOL)isCanGetSMSCode{
    if (![NSString checkPhoneString:self.ui_userTextField.text]) {
        return NO;
    }
    return YES;
}

- (IBAction)getCodeAction:(id)sender {
    if ([self isCanGetSMSCode]) {
        [self showSmsCodeWaitView];
        [self.ui_codeTextField becomeFirstResponder];
        [ServiceManager getMessageCodeWithPhone:self.ui_userTextField.text success:^(NSDictionary *data) {
            self.m_smsResultDic=data;
            DLog(@"发送短信验证码：成功");
            [PromptAction showSuccessPrompt:@"验证码成功发送"];
        } failure:^{
            DLog(@"发送短信验证码：失败");
        }];
    }
    
}
- (BOOL)isCanLogin{
    if (![NSString checkNullString:[DataManager sharedInstance].testAcount]&&[[DataManager sharedInstance].testAcount isEqualToString:self.ui_userTextField.text]&&![NSString checkNullString:self.ui_codeTextField.text]) {
        return YES;
        //特殊帐号验证
    }
    if (![NSString checkPhoneString:self.ui_userTextField.text]){
        [PromptAction showErrorPrompt:@"请输入手机号"];
        return NO;
    }
    if (!self.m_smsResultDic) {
        [PromptAction showErrorPrompt:@"请获取验证码"];
        return NO;
    }
    if ([NSString checkNullString:self.ui_codeTextField.text]) {
        [PromptAction showErrorPrompt:@"请输入验证码"];
        return NO;
    }
    if (![self.ui_codeTextField.text isEqualToString:self.m_smsResultDic[@"identifyingCode"]]) {
        [PromptAction showErrorPrompt:@"验证码错误"];
        return NO;
    }
    if ([NSString checkNullString:self.ui_passWordTextField.text])
    {
        [PromptAction showErrorPrompt:@"请设置密码"];
        return NO;
    }
    if(self.ui_passWordTextField.text.length<6)
    {
        [PromptAction showErrorPrompt:@"密码长度至少6位"];
        return NO;
    }
    return YES;
}
- (IBAction)bgTouchAction:(id)sender {
    [self.view endEditing:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.ui_passWordTextField) {
        if (textField.text.length > 17) return NO;
    }
    
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
